<?php
// +----------------------------------------------------------------------
// |订单管理
// +----------------------------------------------------------------------
// |Data:2016-06-24 下午3:38
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Controller\AdminCommonController;

class OrderController extends AdminCommonController{
    private $model;
    
    protected function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->model = D('Admin/Order');
    }

    public function index(){
        $map = $this->search();
        $list = $this->model->getList($map,'create_time desc');
        foreach ($list['data'] as &$val){
            $val['shop'] = D('Common/Shop')->detail($val['shop_id'])['title'];
        }
        $this->assign($list);

        $this->display('index');
    }

    private function search(){
        $map = array();
        $type = I('get.type',0,'intval');
        if($type){
            $map['status'] = $type;
        }
        
        if(!$this->isSysAdmin){
            $map['shop_id'] = $this->manage_id;
        }

        $s_type = I('get.search_type','','trim');
        $start_time = I('get.start_time','','trim');
        $end_time = I('get.end_time','','trim');
        $keyword = I('get.keywords','','trim');
        $shop_id = I('get.shop_id',0,'intval');

        $keyword = urldecode($keyword);
        if($keyword){
            switch($s_type){
                case 1:$map['orderno'] = array('like','%'.$keyword.'%');break;
                case 2:$map['buyer'] = array('like','%'.$keyword.'%');break;
                default:
                    $map['orderno|buyer'] = array('like','%'.$keyword.'%');
                    break;
            }
        }
        if(!empty($start_time)){
            $map['create_time'] = array('egt',strtotime($start_time));
        }
        $shop_id && $where['shop_id'] = $shop_id;

        if(!empty($end_time)){
            $map['create_time'] = array('lt',strtotime($end_time)+86400);
        }

        if(!empty($start_time) && !empty($end_time)){
            $map['create_time'] = array(array('egt',strtotime($start_time)),array('lt',strtotime($end_time)+86400),'and');
        }

        $this->assign('type',$type);
        $this->assign('search_type',$s_type);
        $this->assign('start_time',$start_time);
        $this->assign('end_time',$end_time);
        $this->assign('keywords',$keyword);
        $this->assign('shop_id',$shop_id);

        $shopList = D('Common/Shop')->getShopList($this->isSysAdmin ? 1 : $this->manage_id);
        $this->assign('shopList',$shopList);

        return $map;
    }

    /**
     * 确认接单
     */
    public function receive(){
        $id = I('post.id',0,'intval');

        if(empty($id)){
            $this->returnData('请选择操作的订单');
        }

        $detail = $this->model->detail($id);
        if(empty($detail)){
            $this->returnData('不存在此订单');
        }

        if($detail['payway'] == 3){
            if($detail['status'] != 1){
                $this->returnData('非法操作');
            }
        }else{
            if($detail['status'] != 2){
                $this->returnData('非法操作');
            }
        }

        $data = array();
        $data['status'] = 3;
        $data['sign_time'] = NOW_TIME;

        $res = $this->model->update($data,$id);

        if($res){
            $this->addHistoryLog('确定接单成功');
            $this->returnData('操作成功',1);
        } else {
            $this->returnData('操作失败');
        }
    }

    /**
     * 确认配送
     */
    public function delivery(){
        $id = I('post.id',0,'intval');

        if(empty($id)){
            $this->returnData('请选择操作的订单');
        }

        $detail = $this->model->detail($id);
        if(empty($detail)){
            $this->returnData('不存在此订单');
        }

        if($detail['status'] != 3){
            $this->returnData('非法操作');
        }

        $data = array();
        $data['status'] = 4;
        $data['deliver_time'] = NOW_TIME;

        $res = $this->model->update($data,$id);

        if($res){
            $this->addHistoryLog('确认订单配送成功');
            $this->returnData('操作成功',1);
        } else {
            $this->returnData('操作失败');
        }
    }

    /**
     * 确认完成
     */
    public function complete(){
        $id = I('post.id',0,'intval');

        if(empty($id)){
            $this->returnData('请选择操作的订单');
        }

        $detail = $this->model->detail($id);
        if(empty($detail)){
            $this->returnData('不存在此订单');
        }

        if($detail['status'] != 4){
            $this->returnData('非法操作');
        }

        $data = array();
        $data['status'] = 5;

        $res = $this->model->update($data,$id);

        if($res){
            $this->addHistoryLog('确认订单完成成功');
            $this->returnData('操作成功',1);
        } else {
            $this->returnData('操作失败');
        }
    }

    /**
     * 确定收款
     */
    public function closed(){
        $id = I('post.id');
        empty($id) && $this->returnData('请选择操作的订单');

        $detail = $this->model->detail($id);
        if(empty($detail)){
            $this->returnData('不存在此订单');
        }

        if($detail['status'] != 5 || $detail['payway'] != 3){
            $this->returnData('非法操作');
        }

        $data = array();
        $data['is_closed'] = 1;

        $res = $this->model->update($data,$id);

        if($res){
            $this->addHistoryLog('确认订单收款成功');
            $this->returnData('操作成功',1);
        } else {
            $this->returnData('操作失败');
        }
    }

    /**
     * 查看评价
     */
    public function evaluate(){
        $id = I('get.id',0,'intval');
        
        if(empty($id)) toErrorPage();

        $detail = $this->model->detail($id);
        if(empty($detail)){
            toErrorPage();
        }
        $this->assign('detail',$detail);

        $evalDetail = $this->model->getOrderEval($id);
        $this->assign('evalDetail',$evalDetail);

        $this->display('evaluate');
    }

    /**
     * 查看详情
     */
    public function detail(){
        $id = I('get.id',0,'intval');

        if(empty($id)) toErrorPage();

        $detail = $this->model->detail($id);
        if(empty($detail)){
            toErrorPage();
        }

        $detail['shop'] = D('Common/Shop')->detail($detail['shop_id'])['title'];
        $this->assign('detail',$detail);
        
        $productList = $this->model->getProductDetail($id);
        if(empty($productList)) toErrorPage();

        foreach ($productList as &$val){
            $val['cate'] = D('Common/ProductCate')->detail($val['product_cid'],'title');
        }

        
        $this->assign('productList',$productList);

        $this->display('detail');
    }
    
    public function doReply(){
        $id = I('post.id',0,'intval');
        $content = I('post.reason');//回复内容

        if(empty($id)){
            $this->returnData('请选择操作的订单');
        }

        $detail = $this->model->detail($id);
        if(empty($detail)){
            $this->returnData('不存在此订单');
        }
        
        if(empty($content)){
            $this->returnData('回复内容不能为空');
        }
        if(abslength($content) > 200){
            $this->returnData('回复内容不能超过200个字');
        }
        
        $res = $this->model->reply($id,$content);

        if($res){
            $this->addHistoryLog('回复评论成功');
            $this->returnData('操作成功',1);
        } else {
            $this->returnData('操作失败');
        }      
    }
}
?>